package com.gzx.spring.db.service.impl;

import java.util.List;

import com.gzx.risk.core.pojo.DailyStats;
import com.gzx.risk.core.pojo.DailyTotalStats;
import org.springframework.stereotype.Service;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzx.risk.core.pojo.OrderInfo;
import com.gzx.risk.core.utils.AESUtils;
import com.gzx.spring.db.entity.PhoneProductOrder;
import com.gzx.spring.db.mapper.PhoneProductOrderMapper;
import com.gzx.spring.db.service.PhoneProductOrderService;

@Service
@DS("phone")
public class PhoneProductOrderServiceImpl extends ServiceImpl<PhoneProductOrderMapper, PhoneProductOrder>
		implements PhoneProductOrderService {

	@Override
	public OrderInfo getOrder(long id) {
		PhoneProductOrder order = this.getById(id);
		return toInfo(order);
	}

	private OrderInfo toInfo(PhoneProductOrder order) {
		OrderInfo info = new OrderInfo();
		info.setId(order.getId());
		info.setOrgId(order.getOrgId());
		info.setUid(order.getUid());
		info.setSid(order.getSid());
		info.setCreatetime(order.getCreatetime());
		info.setLeaseTerm(order.getLeaseTerm());
		info.setOrdercode(order.getOrdercode());
		info.setStatus(order.getStatus());
		info.setPhone(AESUtils.decrypt(order.getPhone()));
		info.setParams(order.getParams());
        info.setCity(order.getCity());
		return info;
	}

	@Override
	public List<OrderInfo> getOrdersByStatus(List<Integer> status) {
		QueryWrapper<PhoneProductOrder> queryWrapper = new QueryWrapper<>();
		queryWrapper.in("status", status);
		queryWrapper.orderByDesc("id");
		List<PhoneProductOrder> orders = list(queryWrapper);
		List<OrderInfo> infos = Lists.newArrayList();
		for (PhoneProductOrder order : orders) {
			infos.add(toInfo(order));
		}
		return infos;
	}

	@Override
	public PhoneProductOrder getOrder(String ordercode) {
		QueryWrapper<PhoneProductOrder> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("ordercode", ordercode);
		PhoneProductOrder order = this.getOne(queryWrapper, false);
		if (order == null) {
			return null;
		}
		order.setPhone(AESUtils.decrypt(order.getPhone()));
		return order;
	}

	@Override
	public OrderInfo getOrderInfo(String ordercode) {
		return toInfo(getOrder(ordercode));
	}

	@Override
	public long getFinishOrderCount(int status) {
		QueryWrapper<PhoneProductOrder> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("status", status);

		if (status == 13) {
			// 逾期
			// 忽略陈俊萍
			queryWrapper.notIn("uid", Lists.newArrayList(11));
		}
		return this.count(queryWrapper);
	}

    @Override
    public List<DailyStats> selectDailyStats(){
        return baseMapper.selectDailyStats();
    }

    @Override
    public List<DailyTotalStats> selectTotalStats(){
        return baseMapper.selectTotalStats();
    }
}
